import sys
import os
import glob
from subprocess import call

SCData = sys.argv[1]
SandData = sys.argv[2]

#finish in 2021.11.12

#function
#files remove 0
def SubToNull(stri, sep):
    #two conditions: 5\t0, 2\t0\t3
        striNew = ""
        striTemp = stri.split(sep)
        subList = ["0", 0, "-"]
        for i in striTemp:
            temp = "%s%s" % (i, sep)
            if i in subList:
                striNew += sep
            else:
                temp = "%s%s" % (i, sep)
                striNew += temp
        striNew = striNew.strip(sep)
        return striNew

def FileRemove(RawFile,FinalFile):
    f=open(RawFile)
    w=file(FinalFile,"w+")
    line=f.readline()
    w.write(line)
    line=f.readline()
    while line:
        line = line.strip("\n")
        striF="%s\n" % (SubToNull(line,"\t"))
        w.write(striF)
        line=f.readline()
    w.close()
    f.close()
#files remove 0

#function

#main
if not os.path.isdir(SandData):
    os.mkdir(SandData)
    print("create dir:",SandData)

if not os.path.isdir(SCData):
    print("single cell analysis data error!")
else:
    print("copy:")
    needfiles1 = ["cluster.png","TCRPriDesign.Final.xls","ClusterTopGene.xls"]
    needfiles2 = ["TCRBarGeneExp.xls"]

    for i in needfiles1:
        filepath = "%s/*%s" % (SCData,i)
        temp = glob.glob(filepath)
        
        for y in temp:
            cmd = "cp %s %s" % (y,SandData)
            print(cmd)
            call(cmd,shell=True)

    print("remove 0 or - in files:")
    for i in needfiles2:
        filepath = "%s/*%s" % (SCData,i)
        temp = glob.glob(filepath)
        for y in temp:
            print(y)
            fileName = os.path.basename(y)
            finalName = "%s/%s" % (SandData,fileName)
            FileRemove(y,finalName)

